<?php
Yii::import('application.vendors.*');
require_once ('Artichow/Plot.class.php');

class HPlotGroup extends PlotGroup {

	private $plot;
	private $last_idx;
	private $keys;
	private $xlabel;

	/**
	 * $arr contains all data. It must be an array like
	 * array(
	 *   array(
	 *     'x_label_1' => numbered_value,
	 *     ...
	 *   ),
	 *   array(
	 *     'x_label_1' => numbered_value,
	 *     ...
	 *   )
	 * )
	 */
	public function initialize($data) {

		if (is_array($data)) {
			// plots definition
			$this->plots = array ();
			// last key index found for each plot
			$this->last_idx = array ();
			// list all keys of all data
			$this->keys = array ();
			
			$this->xlabel = array();
			// 1st we merge all data keys
			foreach ($data as $pIdx => $plot) {
				if (isset($plot['data']))
				{
					$this->plots[$pIdx] = array ();
					$this->last_idx[$pIdx] = -1;
					$this->keys = array_merge($this->keys, array_keys($plot['data']));
				}
			}

			$this->keys = array_unique($this->keys);
			sort($this->keys);

			// for each key
			//$this->plots[-1][-1] = 0;
			//$this->last_idx[-1] = -1;
			foreach ($this->keys as $kIdx => $key) {
				foreach ($data as $pIdx => $plot) {
					if (isset ($plot['data'][$key])) {
						if ($this->last_idx[$pIdx] != $kIdx -1) {
							// some data missing.... we compute them
							$this->addMissingData($plot['data'], $this->last_idx[$pIdx], $kIdx, $this->plots[$pIdx]);
						}
						$this->plots[$pIdx][$kIdx] = $plot['data'][$key];
						$this->last_idx[$pIdx] = $kIdx;
						// TODO Verifier !!
						if (isset($data['xlabel'][$key]))
							$this->xlabel[$kIdx] = $data['xlabel'][$key];
						else
							$this->xlabel[$kIdx] = $pIdx;
					}
				}
			}
		}

		foreach ($this->plots as $pIdx => $values) {
			$plot = new LinePlot($values);
			$plot->setBackgroundGradient(new LinearGradient(new Color(210, 210, 210), new Color(250, 250, 250), 0));
			$plot->yAxis->setLabelPrecision(0);
			//$plot->yAxis->setLabelNumber(0);
			$plot->setSpace(5, 5, NULL, NULL);
			$plot->xAxis->setLabelText($this->keys);
			if (isset ($data[$pIdx]['color'])) {
				$plot->setColor($data[$pIdx]['color']);
			}
			$this->add($plot);
			if (isset($data[$pIdx]) && isset($data[$pIdx]['legend']))
				$this->legend->add($plot, $data[$pIdx]['legend'], Legend::MARK);
		}

//$this->legend->setPosition(1.18);
//$this->legend->setTextFont(new Tuffy(8));
$this->legend->setSpace(10);

		$this->axis->left->setLabelPrecision(0);
		$this->axis->left->setLabelNumber(3);
		$this->axis->bottom->setLabelText($this->xlabel);
	}

	private function addMissingData($plot, $pIdx, $kIdx, & $out) {
		$start = 0;
		if ($pIdx > 0)
			$start = $plot[$this->keys[$pIdx]];
		if ($pIdx < 0)
			$pIdx = 0;
		$end = $plot[$this->keys[$kIdx]];

		$gen = $kIdx - $pIdx;
		$step = ($end - $start) / $gen;

		while ($pIdx != $kIdx) {
			$out[$pIdx] = $start;
			$pIdx = $pIdx +1;
			$start = $start + $step;
		}
	}

	private function getColor($color) {
		if ($color instanceof Color) {
			return $color;
		} else {
			if (is_array($color)) {
				$red = $this->findColorValue($color, array (
					'red',
					'RED',
					'r',
					'R',
					0
				));
				$green = $this->findColorValue($color, array (
					'green',
					'GREEN',
					'g',
					'G',
					1
				));
				$blue = $this->findColorValue($color, array (
					'blue',
					'BLUE',
					'b',
					'B',
					1
				));

				return new Color($red, $green, $blue);
			} else {
				return new Color(0, 0, 0);
			}
		}
	}

	private function findColorValue($arr, $keys) {
		foreach ($keys as $key) {
			if (isset ($arr[$key]))
				return $arr ($key);
		}

		return 0;
	}
}
?>
